<?php

/**
 * Controller qui gère la configuration à appliquer
 */

namespace Acme\DomotiqueBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Acme\DomotiqueBundle\Entity\Data;
use Acme\DomotiqueBundle\Entity\Commande;
use Acme\DomotiqueBundle\Entity\Element;

class ArduinoController extends Controller
{

	public function getAction($arduino_id)
	{

		/**
		 * récupère les commandes à executer
		 * Met a jour la valeur de l'élement
		 */
		$em = $this->getDoctrine()->getManager();
		$commandes = $em->getRepository("DomotiqueBundle:Commande")
					->findBy(
						array(
							"done" => false,
							"arduino" => $arduino_id
						)
					);
		$response = array();
		foreach($commandes as $commande)
		{
			$commande->setDone(true);

			$element = $this->getDoctrine()->getManager()->getRepository("DomotiqueBundle:Element")
				->findOneBy(
					array(
						"arduino" => $arduino_id, 
						"pin_id" => $commande->getPinId()
					)
				);
			$element->setCurrentValue($commande->getValue());

			$em->persist($element);
			$em->persist($commande);
			$response[] .= $commande->getPinId().";".$commande->getActionName().";".$commande->getValue();
		}

		$em->flush();

		return new Response(implode("#",$response));
	}
	/**
	 * Ajoute une entrée de capteur 
	 */
	public function feedAction($arduino_id, $pin, $value)
	{
		$data = new Data();
		$data->setArduinoId($arduino_id);
		$data->setPinId($pin);
		$data->setValue($value);

		$em = $this->getDoctrine()->getManager();
		$em->persist($data);
		$em->flush();

		return new Response(json_encode(array("response_code" => "ok")));
	}
	/**
	 * Ajoute une nouvelle commande 
	 */
	public function newCommandeAction($element_id, $type, $value)
	{

		$em = $this->getDoctrine()->getManager();

		// get element
		$element = $this->getDoctrine()->getManager()->getRepository("DomotiqueBundle:Element")
				->findOneBy(array("id" => $element_id));

		if($type == "intensity"){
			if($value > $element->getCurrentValue()){
				$action = "fadeIn";
			}else{
				$action = "fadeOut";
			}
			$element->setCurrentValue($value);
		}elseif($type == "color"){
			$element->setCurrentColor($value);
			$action = "";
		}elseif($type == "state"){
			$element->setCurrentValue($value);
			$action = "";
		}
		$current_value = $element->getCurrentValue();


		// update les anciennes commandes
		$commandes = $this->getDoctrine()->getManager()->getRepository("DomotiqueBundle:Commande")
				->findBy(array("arduino" => $element->getArduino(), "pin_id" => $element->getPinId(), "action_type" => $type));
		foreach($commandes as $commande){
			$commande->setDone(true);
			$em->persist($commande);
		}

		$commande = new Commande();
		$commande->setArduino($element->getArduino());
		$commande->setPinId($element->getPinId());
		$commande->setActionName($action);
		$commande->setActionType($type);
		$commande->setValue($value);

		$em->persist($commande);
		$em->persist($element);
		$em->flush();


		return new Response(json_encode(array("response_code" => "ok")));

	}
    
}